class AccountController < ApplicationController
    
    def admin
        #Render all user account whose are assigned to this project.
        @project = Project.find_by_id(current_user.project.id)
        @resource = Resource.new
              
    end
    
    def projectConfiguration
    
        if params[:project]
            flash[:notice] = params[:project][:time_zone] + " ? " + current_user.id.to_s
                    
        @project = Project.new(params[:project])
        @user = User.find(current_user.id)
        
        if @project.save
            @user.project = @project
            @user.save
	        flash[:notice] = "Project is now saved to your profile."
            redirect_to root_path 

        else
	        flash[:notice] = "Project details cannot be created."
            redirect_to root_path 

        end
     end        
  end
  
    def createProjectAccount
        @params = params[:account]
        @params[:password] = "Welcome01"
        @params[:password_confirmation] = "Welcome01"
        @account = User.new(@params)
        @account.project = current_user.project
        @account.permission = Permission.new
        if @account.save
            UserMailer.registration_confirmation(current_user).deliver
            flash[:notice] = @account[:email].to_s + " is successfully created"
        else
            flash[:notice] = @account[:email].to_s + " creation failed"   
        end
        redirect_to :back 
    end
    
    def editAccountPermission
        @account = User.new(params[:account])
        @account.project = current_user.project
        @account.permission = Permission.new
        if @account.save
            flash[:notice] = @account[:email].to_s + " is successfully created"
        else
            flash[:notice] = @account[:email].to_s + " creation failed"   
        end
        redirect_to :back 
    end

  def newworkordertype
     @workordertype = Workordertype.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @workordertype }
    end 
  end

  def newresource
     @resourcegroup_data = Resourcegroup.find(:all)
     @resource = Resource.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @resource }
    end 
  end  

  def newresourcegroup
 
     @resourcegroup = Resourcegroup.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @resource }
    end 
  end
  
  def createworkordertype
    #puts 'HAHAHAHAHAHAHAHHA =' + params[:workordertype].type.to_s;
	@workordertype = Workordertype.new(params[:workordertype])
	
    respond_to do |format|
      if @workordertype.save
	flash[:notice] = "Workorder type successfully created"
        format.html { render :controller => "account" }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
		flash[:notice] = "Workorder type cannot be created"
        format.html { render :controller => "account" , :action => "newworkordertype" }
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end
  
    def createresource
    #puts 'HAHAHAHAHAHAHAHHA =' + params[:workordertype].type.to_s;
	@resource = Resource.new(params[:resource])
	
    respond_to do |format|
      if @resource.save
        current_user.project.resources << @resource
        current_user.project.save
	flash[:notice] = "Resource successfully created"
        format.html { redirect_to :controller => "account" , :action => "admin" }
      else
	flash[:notice] = "Resource cannot be created"
        format.html { redirect_to :controller => "account",:action => "admin" }
      end
    end
  end

    def createresourcegroup
    #puts 'HAHAHAHAHAHAHAHHA =' + params[:workordertype].type.to_s;
	@resourcegroup = Resourcegroup.new(params[:resourcegroup])
	
    respond_to do |format|
      if @resourcegroup.save
	flash[:notice] = "Resource successfully created"
        format.html { render :controller => "account" , :action => "newresourcegroup" }
        #format.xml  { render :xml => @event, :status => :created, :location => @event }
      else
	flash[:notice] = "Resource group cannot be created"
        format.html { render :controller => "account" , :action => "newresourcegroup" }
        #format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
      end
    end
  end 
    
    
end
